package com.yy.util.common;

import android.content.Context;
import android.inputmethodservice.InputMethodService;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.Toast;

/**
 * Created by zhangly on 2015/6/1.
 */
public class WindowUtil {

    private static int[] screenPx;

    public static int[] getScreenPx(Context context) {
        if(screenPx == null) {
            screenPx = new int[2];
            WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            DisplayMetrics metrics = new DisplayMetrics();
            manager.getDefaultDisplay().getMetrics(metrics);
            screenPx[0] = metrics.widthPixels;
            screenPx[1] = metrics.heightPixels;
        }
        return screenPx;
    }

    public static void hintInputMethod(final Context context, final EditText editText) {
        Handler handler = new Handler();
        handler.post(new Runnable() {
            @Override
            public void run() {
                InputMethodManager manager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
                manager.hideSoftInputFromInputMethod(editText.getWindowToken(), 0);
            }
        });
    }

    public static void showInputMethod(final Context context, final EditText editText) {
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                InputMethodManager manager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
                manager.showSoftInput(editText, 0);
            }
        }, 200);
    }

    public static void toast(Context context, String message) {
        Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
    }

    public static void toast(Context context, int resId) {
        Toast.makeText(context, resId, Toast.LENGTH_SHORT).show();
    }

}
